import paramiko


def ssh_connect(host, port=22, username=None, password=None, key_filename=None):
    """建立SSH连接"""
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        if key_filename:
            ssh.connect(host, port, username, key_filename=key_filename)
        else:
            ssh.connect(host, port, username, password)
        print(f"成功连接到服务器: {host}")
        return ssh
    except Exception as e:
        print(f"SSH连接失败: {str(e)}")
        return None